#include "Window.hpp"

Window::Window(::HWND handle)
{
  SetHandle(handle);
}

void Window::SetHandle(::HWND handle)
{
  m_handle = handle;
}

::HWND Window::GetHandle()
{
  return m_handle;
}

File* Window::GetFile()
{
  unsigned long processId;
  ::HANDLE process;

  // TODO: make length dynamic
  char* processFile = new char[MAX_PATH + 1];
   
  // get process id
  ::GetWindowThreadProcessId(m_handle, &processId);

  // open process
  process = ::OpenProcess(PROCESS_ALL_ACCESS, false, processId); // get permissions later if necessary
  if (process != NULL)
  {  
    // if it fails, set to empty string
    if (GetProcessImageFileName(process, processFile, MAX_PATH) == 0)
      processFile[0] = '\0';

    ::CloseHandle(process);
  }

  if (processFile[0] == '\0')
    return NULL;
  return new File(processFile);
}

bool Window::operator==(const Window& window1, const Window& window2)
{
  return (window1.GetHandle() == windo2.GetHandle());
}